package com.zzyl.serve.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;
    @Autowired
    private RedisTemplate redisTemplate;
    private static final String CACHE_KEY_PREFIX = "nursingPlan:all";

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Integer id)
    {
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        NursingPlan nursingPlan = getById(id);
        BeanUtils.copyProperties(nursingPlan,nursingPlanVo);
        //根据护理计划id查询中间表中关联的护理项目计划，并进行数据封装
        List<NursingProjectPlan> nursingProjectPlans = nursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        //类型转换
        List<NursingProjectPlanVo> nursingProjectPlansVo = nursingProjectPlans.stream().map(nursingProjectPlan -> {
            NursingProjectPlanVo nursingProjectPlanVo = new NursingProjectPlanVo();
            nursingProjectPlanVo.setProjectId(String.valueOf(nursingProjectPlan.getProjectId()));
            nursingProjectPlanVo.setExecuteCycle(nursingProjectPlan.getExecuteCycle() + "");
            nursingProjectPlanVo.setExecuteTime(nursingProjectPlan.getExecuteTime());
            nursingProjectPlanVo.setExecuteFrequency(Long.valueOf(nursingProjectPlan.getExecuteFrequency()));
            nursingProjectPlanVo.setId(Long.valueOf(nursingProjectPlan.getId()));
            nursingProjectPlanVo.setPlanId(Long.valueOf(nursingProjectPlan.getPlanId()));
            return nursingProjectPlanVo;
        }).collect(Collectors.toList());


        nursingPlanVo.setProjectPlans(nursingProjectPlansVo);
        return nursingPlanVo;
    }
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        //判断缓存中是否含有护理计划缓存
        List<NursingPlan> nursingPlans = (List<NursingPlan>) redisTemplate.opsForValue().get(CACHE_KEY_PREFIX);
        if(nursingPlans != null){
            return nursingPlans;
        }
        //从数据库中查询到对应的护理计划
        nursingPlans = nursingPlanMapper.selectNursingPlanList(nursingPlan);
        //将护理计划缓存添加到redis中
        redisTemplate.opsForValue().set(CACHE_KEY_PREFIX,nursingPlans);
        return nursingPlans;
    }

    /**
     * 新增护理计划
     * 
     * @param nursingPlan 护理计划
     * @return 结果
     */
    @Override
    public int insertNursingPlan(NursingPlan nursingPlan)
    {
        boolean flag = save(nursingPlan);
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return  flag? 1 : 0;
    }

    /**
     * 修改护理计划
     * 
     * @param nursingPlan 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlan nursingPlan)
    {
        boolean flag = updateById(nursingPlan);
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return flag ? 1 : 0;
    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
        boolean flag = removeByIds(Arrays.asList(ids));
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return  flag? 1 : 0;
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        boolean flag = removeById(id);
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return  flag? 1 : 0;
    }
}
